<template>
  <el-dialog title="积分赠送新增" :close-on-click-modal="false" :visible.sync="visible" center fullscreen>
		<el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-form :model="dataForm" ref="dataForm" :rules="dataRule" label-width="110px" size="mini">
      <el-row>
        <el-col :span="8">
          <el-form-item label="会员卡号" prop="customerCard">
            <el-input v-model="dataForm.customerCard" readonly>
              <el-button slot="append" icon="el-icon-search" @click="queryCustomerList">选择</el-button>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="会员姓名" prop="customerName">
            <el-input v-model="dataForm.customerName" readonly></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="会员电话" prop="phone1">
            <el-input v-model="dataForm.phone1" readonly></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="医院积分" prop="integral">
            <el-input v-model="dataForm.integral" readonly></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="门店积分" prop="storesIntegral">
            <el-input v-model="dataForm.storesIntegral" readonly></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="赠送积分" prop="changeIntegral">
            <el-input v-model="dataForm.changeIntegral"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="memo">
            <el-input v-model="dataForm.memo" type="textarea"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="dataFormSubmit" :loading="ischeck" v-prevent-re-click>保 存</el-button>
    </span>
    <customer-search v-if="customerSearchVisible" ref="customerSearch" @refreshForm="updateForm"></customer-search>
  </el-dialog>
</template>

<script>
  import {customerIntegralSave} from '@/api/eims/customer-integral'
  import {fetchCustomerList} from '@/api/eims/customer'
  import CustomerSearch from '@/components/business/customer-search'

  export default {
    data() {
      return {
        ischeck: false,
        visible: false,
        dataForm: {
          customerCard: '',
          customerName: '',
          phone1: '',
          integral: '',
          storesIntegral: '',
          changeIntegral: '',
          memo: ''
        },
        dataRule: {
          changeIntegral: [{required: true,message: '请填写赠送积分'}]
        },
        customerSearchVisible: false
      }
    },
    components: {
      CustomerSearch
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.switchValue = false
        this.fromEdit = false
        this.dataForm.customerId = ''
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.fromEdit = true
            customerComplaintInfo(this.dataForm.id).then(response => {
              if(response.data.code == '0'){
                this.dataForm = response.data.data
              }else{
                this.$message.error(response.data.msg)
              }
            })
          }
        })
      },
      // 查询会员表
      queryCustomerList() {
        this.customerSearchVisible = true
        this.$nextTick(() => { this.$refs.customerSearch.init(this.dataForm.customerId) })
      },
      // 选择会员
      updateForm (data) {
        this.dataForm.customerCard = data.customerCard
        this.dataForm.customerName = data.name
        this.dataForm.phone1 = data.phone1
        this.dataForm.integral = data.integral || 0
        this.dataForm.storesIntegral = data.storesIntegral || 0
        this.dataForm.customerId = data.id
      },
      // 增加、修改会员回访记录
      dataFormSubmit() {
        this.ischeck = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            customerIntegralSave(this.dataForm).then(result => {
              this.$message.success('新增成功')
              this.visible = false
              this.$emit('refreshData')
              setTimeout(() => {
                this.ischeck = false
              },1000)
            }).catch(() => {
              this.ischeck = false
            })
          }else{
            this.ischeck = false
          }
        })
      },

    }
  }
</script>

<style>
</style>
